package com.luoqian.demo01;

import java.lang.reflect.Method;
import java.util.ArrayList;

/**
 * 泛型擦除
 * 泛型只在编译阶段有效，编译之后JVM会采取去泛型化的措施
 */
public class RemoveGenerics {
    public static void main(String[] args) throws Exception {
        ArrayList<String> stringArrayList = new ArrayList<>();
        stringArrayList.add("123");
        stringArrayList.add("456");
        stringArrayList.add("789");
        //stringArrayList.add(new Object());
        System.out.println(stringArrayList);
        //通过反射添加元素到集合中
        Class<? extends ArrayList> aClass = stringArrayList.getClass();
        Method method = aClass.getDeclaredMethod("add", Object.class);
        method.invoke(stringArrayList,new Object());
        System.out.println(stringArrayList);
    }
}
